package com.kichina.galaxy.dev.tools.controller.base;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Surging
 * @version 1.0
 * @ClassName ISocketMessageHandler
 * @date 2022-06-16 14:01
 * @desc  基本控制层，基本方法类
 */

@Slf4j
public abstract class BaseController {



    public BaseController() {
    }



    /*之前的数据处理  成功*/
    protected ResponseEntity<ModelMap> SuccessModelMap() {
        return this.SuccessModelMap(new ModelMap(), (Object) null);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(ModelMap modelMap) {
        return this.SuccessModelMap(modelMap, (Object) null);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(Object data) {
        return this.setModelMap(new ModelMap(), HttpCode.OK, data);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(ModelMap modelMap, Object data) {
        return this.setModelMap(modelMap, HttpCode.OK, data);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(String message) {
        return this.setModelMap(HttpCode.OK, message);

    }

    protected ResponseEntity<ModelMap> SuccessModelMap(String message, Object data) {
        return this.setModelMap(HttpCode.OK, message, data);

    }
    /*失败*/
    protected ResponseEntity<ModelMap> FailModelMap() {
        return this.FailModelMap(new ModelMap(), (Object) null);
    }

    protected ResponseEntity<ModelMap> FailModelMap(ModelMap modelMap) {
        return this.FailModelMap(modelMap, (Object) null);
    }

    protected ResponseEntity<ModelMap> FailModelMap(Object data) {
        return this.setModelMap(new ModelMap(), HttpCode.NO_DATA, data);
    }

    protected ResponseEntity<ModelMap> FailModelMap(ModelMap modelMap, Object data) {
        return this.setModelMap(modelMap, HttpCode.NO_DATA, data);
    }

    protected ResponseEntity<ModelMap> FailModelMap(String message) {
        return this.setModelMap(HttpCode.NO_DATA, message);
    }

    protected ResponseEntity<ModelMap> FailModelMap(String message, Object data) {
        return this.setModelMap(HttpCode.NO_DATA, message, data);
    }


    protected ResponseEntity<ModelMap> setModelMap(HttpCode code) {
        return this.setModelMap((ModelMap) (new ModelMap()), (HttpCode) code, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(String code, String message) {
        return this.setModelMap(new ModelMap(), code, message, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, String message) {
        return this.setModelMap(new ModelMap(), code.code().toString(), message, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, String message, Object data) {
        return this.setModelMap(new ModelMap(), code.code().toString(), message, data);
    }

    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code) {
        return this.setModelMap((ModelMap) modelMap, (HttpCode) code, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, Object data) {
        return this.setModelMap(new ModelMap(), code, data);
    }

    protected ResponseEntity<ModelMap> setModelMap(String code, String message, Object data) {
        return this.setModelMap(new ModelMap(), code, message, data);
    }

    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code, Object data) {
        return this.setModelMap(modelMap, code.code().toString(), code.reasonPhraseCN(), data);
    }
    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, String code, String message, Object data) {
        if (!modelMap.isEmpty()) {
            Map<String, Object> map = new LinkedHashMap<>(modelMap);
            modelMap.clear();
            for (String key : map.keySet()) {
                if (!key.startsWith("org.springframework.validation.BindingResult") && !key.equals("void")) {
                    modelMap.put(key, map.get(key));
                }
            }
        }
        if (StringUtils.isBlank(message)) {
            message = "";
        }
        modelMap.put("success",HttpCode.OK.code().toString().equals(code));
        modelMap.put("message", message);
        modelMap.put("code", code);
        modelMap.put("timestamp", System.currentTimeMillis());
        if (data != null) {
            modelMap.put("data", data);
        }
        log.debug("response===>{}", JSON.toJSONString(modelMap));
        return ResponseEntity.ok(modelMap);
    }
}
